var trades = [];

var expandList = function(type) {
	var food = document.getElementById(type);
	var click = food.getAttribute("data-click");

	if (click == 0) {
		// 点开
		food.setAttribute("data-click", 1);
		$api.dom('#' + type + 'arrow').src = "../image/ic_arrow_up_black.png";
		food.style.display = 'block';
	} else {
		// 关闭
		food.setAttribute("data-click", 0);
		$api.dom('#' + type + 'arrow').src = "../image/ic_arrow_down_black.png";
		food.style.display = 'none';
	}
}
var apiready = function() {
	getTrades(function(result, err) {
		if (result) {
			trades = result.values;
			renderExpandList();
		} else {
			api.alert({
				msg: ('错误码：' + err.code + '；错误信息：' + err.msg + '网络状态码：' + err.statusCode)
			});
		};
	});
};

var renderExpandList = function() {
	var $body = $api.dom('body');
	trades.some(function(item, i) {

		var tradesArr = [];
		tradesArr.push('<div class="classify" tapmode onclick="expandList(\'price_' + item.name + '\')">');
		tradesArr.push('<img src="' + SERVER_URL + '/entity/file/' + item.icon + '" alt="" class="sublogo">');
		tradesArr.push('<div class="info">');
		tradesArr.push('<div class="title">' + item.name + '</div>');
		tradesArr.push('</div>');
		tradesArr.push('<img src="../image/ic_arrow_down_black.png" alt="" class="arrow" id="price_' + item.name + 'arrow">');
		tradesArr.push('</div>');

		$api.append($body, tradesArr.join(''));

		var priceHtml = '<div class="subclass" id="price_' + item.name + '" data-click="0">';
		item.price.some(function(priceItem, j) {
			var priceArr = [];
			priceArr.push('<div class="row">');
			priceArr.push('<div class="col flipcard bb">');
			priceArr.push(priceItem.name);
			priceArr.push(priceItem.money + '$/' + priceItem.time + 'min');
			priceArr.push('</div>');
			priceArr.push('</div>');
			priceHtml += priceArr.join('');
		});
		priceHtml += '</div>';

		$api.append($body, priceHtml);
	});
};

var getTrades = function(callback) {
	api.ajax({
		url: SERVER_URL + '/entity/config/trades',
		method: 'get',
		timeout: 30,
		dataType: 'json',
		returnAll: false,
		data: {}
	}, callback);
};
